package algorithm;

public class leetcode_0200 {
    public int numIslands(char[][] grid) {
        int m=grid.length;
        int n=grid[0].length;
        int count=0;
        int[][] copy=new int[m][n];
        for(int i=0;i<m;++i){
            for(int j=0;j<n;++j){
                if(grid[i][j]=='1'&&copy[i][j]==0){
                    count++;
                    setone(copy,i,j,m,n,grid);
                }
            }
        }
        return count;
    }
    public void setone(int[][] copy,int i,int j,int m,int n,char[][] grid){
        if(copy[i][j]==1) return;
        copy[i][j]=1;
        if(i>0&&grid[i-1][j]=='1'){
            setone(copy,i-1,j,m,n,grid);
        }
        if(j>0&&grid[i][j-1]=='1'){
            setone(copy,i,j-1,m,n,grid);
        }
        if(j+1<n&&grid[i][j+1]=='1'){
            setone(copy,i,j+1,m,n,grid);
        }
        if(i+1<m&&grid[i+1][j]=='1'){
            setone(copy,i+1,j,m,n,grid);
        }
    }
}
